import {
    getUserResumesInfoById,
    saveUserResumeInfo,
    saveUserResumeInfoAvatar,
    downloadUserResumeFileById,
    deleteUserOnlineResumeFileById,
    modifyOrSaveUserResumeEduExperienceInfo,
    modifyOrSaveUserResumePraExperienceInfo,
    modifyOrSaveUserResumeProExperienceInfo,
    getUserResumeInfoByUserIdAndResumeId,
    deleteEduExperienceById,
    deletePraExperienceById,
    deleteProExperienceById,
    modifyUserResumeInfo,
    deleteUserResumeById,
    modifyUserResumeHonorAward,
    modifyUserResumeHobby,
    userDeliverResume,
    checkUserDeliveredResumeForCareer
} from "@/api/resume";

const state = {
    resumesInfo: []
}

const mutations = {

    SET_RESUMEINFO(state, data) {
        state.resumesInfo = data
    }
}

const actions = {

    getUserResumesInfoById({commit}, id) {
        return new Promise((resolve, reject) => {
            getUserResumesInfoById(id)
                .then((resp) => {
                    const {data} = resp
                    if (data.code === 1) {
                        commit("SET_RESUMEINFO", data.data)
                        resolve(true)
                        return
                    }
                    reject(false)
                })
                .catch((error) => {
                    reject(false)
                })
        })
    },

    getUserResumeInfoByUserIdAndResumeId({commit}, condition) {
        return new Promise((resolve, reject) => {
            getUserResumeInfoByUserIdAndResumeId(condition.id, condition.resumeId)
                .then((resp) => {
                    const {data} = resp
                    if (data.code === 1) {
                        resolve(data.data)
                    } else if (data.code === 0) {
                        reject(data.msg)
                    }
                    reject(false)
                })
                .catch((error) => {
                    reject(false)
                })
        })
    },

    saveUserResumeInfo({commit}, data) {
        return new Promise((resolve, reject) => {
            saveUserResumeInfo(data)
                .then((resp) => {
                    if (resp.data.data.flag) {
                        resolve(resp.data.data)
                        return
                    }
                    reject("简历信息添加失败")
                })
                .catch((error) => {
                    reject(error)
                })
                .finally(() => {
                })
        })
    },

    modifyUserResumeInfo({commit}, data) {
        return new Promise((resolve, reject) => {
            modifyUserResumeInfo(data)
                .then((resp) => {
                    if (resp.data.data) {
                        resolve(true)
                    }
                    reject("简历信息修改失败")
                })
                .catch((error) => {
                    reject(error)
                })
                .finally(() => {
                })
        })
    },

    downloadUserResumeFileById({commit}, data) {
        return new Promise((resolve, reject) => {
            downloadUserResumeFileById(data)
                .then((resp) => {
                    resolve(true)
                })
                .catch((error) => {
                    reject(error)
                })
                .finally(() => {
                })
        })
    },

    deleteUserOnlineResumeFileById({commit}, id) {
        return new Promise((resolve, reject) => {
            deleteUserOnlineResumeFileById(id)
                .then((resp) => {
                     resolve(true)
                })
                .catch((error) => { reject(error) })
                .finally(() => {})
        })
    },

    modifyOrSaveUserResumeEduExperienceInfo({commit}, data) {
        return new Promise((resolve, reject) => {
            modifyOrSaveUserResumeEduExperienceInfo(data)
                .then((resp) => {
                    resolve(true)
                })
                .catch((error) => { reject(error) })
                .finally(() => {})
        })
    },

    modifyOrSaveUserResumePraExperienceInfo({commit}, data) {
        return new Promise((resolve, reject) => {
            modifyOrSaveUserResumePraExperienceInfo(data)
                .then((resp) => {
                    resolve(true)
                })
                .catch((error) => { reject(error) })
                .finally(() => {})
        })
    },

    modifyOrSaveUserResumeProExperienceInfo({commit}, data) {
        return new Promise((resolve, reject) => {
            modifyOrSaveUserResumeProExperienceInfo(data)
                .then((resp) => {
                    resolve(true)
                })
                .catch((error) => { reject(error) })
                .finally(() => {})
        })
    },

    deleteEduExperienceById({commit}, id) {
        return new Promise((resolve, reject) => {
            deleteEduExperienceById(id)
                .then((resp) => {
                    if (resp.data.data) {
                        resolve(true)
                    }
                    reject(false)
                })
                .catch((error) => { reject(error) })
                .finally(() => {})
        })
    },

    deletePraExperienceById({commit}, id) {
        return new Promise((resolve, reject) => {
            deletePraExperienceById(id)
                .then((resp) => {
                    if (resp.data.data) {
                        resolve(true)
                    }
                    reject(false)
                })
                .catch((error) => { reject(error) })
                .finally(() => {})
        })
    },

    deleteProExperienceById({commit}, id) {
        return new Promise((resolve, reject) => {
            deleteProExperienceById(id)
                .then((resp) => {
                    if (resp.data.data) {
                        resolve(true)
                    }
                    reject(false)
                })
                .catch((error) => { reject(error) })
                .finally(() => {})
        })
    },

    deleteUserResumeById({commit}, id) {
        return new Promise((resolve, reject) => {
            deleteUserResumeById(id)
                .then((resp) => {
                    if (resp.data.data) {
                        resolve(true)
                    }
                    reject(false)
                })
                .catch((error) => { reject(error) })
                .finally(() => {})
        })
    },

    modifyUserResumeHonorAward({commit}, data) {
        return new Promise((resolve, reject) => {
            modifyUserResumeHonorAward(data)
                .then((resp) => {
                    if (resp.data.data) {
                        resolve(true)
                    }
                    reject('系统error')
                })
                .catch((error) => {
                    reject(error)
                })
        })
    },

    modifyUserResumeHobby({commit}, data) {
        return new Promise((resolve, reject) => {
            modifyUserResumeHobby(data)
                .then((resp) => {
                    if (resp.data.data) {
                        resolve(true)
                    }
                    reject('系统error')
                })
                .catch((error) => {
                    reject(error)
                })
        })
    },

    userDeliverResume({commit}, data) {
        return new Promise((resolve, reject) => {
            userDeliverResume(data)
                .then((resp) => {
                    if (resp.data.data) {
                        resolve(true)
                    }
                    if (resp.data.code === 0) {
                        reject(resp.data.msg)
                    }
                    reject('简历投递失败！')
                })
                .catch((error) => {
                    reject(error)
                })
        })
    },

    checkUserDeliveredResumeForCareer({commit}, data) {
        return new Promise((resolve, reject) => {
            checkUserDeliveredResumeForCareer(data)
                .then((resp) => {
                    if (resp.data.code === 1) {
                        resolve(resp.data)
                    } else if (resp.data.code === 0) {
                        reject(resp.data.msg)
                    }
                    reject('状态查询失败！')
                })
                .catch((error) => {
                    reject(error)
                })
        })
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
